package com.wangjiang.foundation.mybatis.controller;

import com.wangjiang.foundation.common.rest.ResultGenerator;
import com.wangjiang.foundation.common.rest.entity.Result;
import com.wangjiang.foundation.mybatis.entity.BaseEntity;
import com.wangjiang.foundation.mybatis.entity.BaseListRet;
import com.wangjiang.foundation.mybatis.entity.BaseVO;
import com.wangjiang.foundation.mybatis.params.IdList;
import com.wangjiang.foundation.mybatis.params.QueryParams;
import com.wangjiang.foundation.mybatis.service.CurdService;
import com.wangjiang.foundation.mybatis.utils.QueryParamsUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.Validate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

public interface CurdController<E extends BaseEntity, V extends BaseVO<E>> {

    CurdService<E, V> getService();

    /**
     * 列表查询 分页  paging=false
     *
     * f_like_configName=&sort_id=desc 默认按照创建时间倒序排列
     *
     * @author wangjiang
     * @date 2021-01-02 11:17:28
     */
    @GetMapping(path = "")
    @ApiOperation("列表查询")
    default Result<BaseListRet<V>> list(QueryParams queryParams, HttpServletRequest request) {
        QueryParamsUtil.paddingTerms(queryParams, request);
        QueryParamsUtil.paddingSortParam(queryParams, request);
        BaseListRet<V> result = getService().getList(queryParams);
        return ResultGenerator.genSuccessResult(result);
    }

    /**
     * id查询
     *
     * @author wangjiang
     * @date 2021-01-02 11:17:28
     */
    @GetMapping(path = "/{id:.+}")
    @ApiOperation("根据主键查询")
    default Result<V> getByPrimaryKey(@PathVariable String id) {
        V result = getService().getByUuid(id);
        return ResultGenerator.genSuccessResult(result);
    }

    /**
     * id查询
     *
     * @author wangjiang
     * @date 2021-01-02 11:17:28
     */
    @GetMapping(path = "/id/{id:.+}")
    @ApiOperation("根据id查询")
    default Result<V> getById(@PathVariable Long id) {
        V result = getService().getById(id);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping
    @ApiOperation(value = "创建数据", responseReference = "add")
    @ApiResponses({
            @ApiResponse(code = 201, message = "创建成功,返回创建数据的ID"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "无权限")
    })
    default Result<Boolean> add(@RequestBody V data) {
        return ResultGenerator.genSuccessResult(getService().insert(modelToEntity(data)));
    }


    @PutMapping(path = "/{id}")
    @ApiOperation("根据ID修改数据")
    default Result<Boolean> updateByPrimaryKey(@PathVariable String id, @RequestBody V data) {
        Validate.notNull(data, "update data cannot be null.");
        return ResultGenerator.genSuccessResult(getService().update(id, modelToEntity(data)));
    }

    @PatchMapping(path = "/{id}")
    @ApiOperation("根据ID部分修改数据")
    default Result<Boolean> patchByPrimaryKey(@PathVariable String id, @RequestBody(required = false) V data) {
        Validate.notNull(data, "patch data cannot be null.");
        return ResultGenerator.genSuccessResult(getService().patch(id, modelToEntity(data)));
    }

    @PutMapping
    @ApiOperation("保存数据,如果数据不存在则新增一条数据")
    default Result<Boolean> saveOrUpdate(@RequestBody V data) {
        return ResultGenerator.genSuccessResult(getService().saveOrUpdate(modelToEntity(data)));
    }


    @DeleteMapping(path = "/{id:.+}")
    @ApiOperation("根据ID删除数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 404, message = "要删除的数据不存在")
    })
    default Result<Boolean> deleteByPrimaryKey(@PathVariable String id) {
        return ResultGenerator.genSuccessResult(getService().delete(id));
    }

    @DeleteMapping(path = "")
    @ApiOperation("根据ID删除数据")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 401, message = "未授权"),
            @ApiResponse(code = 403, message = "无权限"),
            @ApiResponse(code = 404, message = "要删除的数据不存在")
    })
    default Result<Boolean> deleteByIds(@RequestBody IdList ids) {
        return ResultGenerator.genSuccessResult(getService().deleteByIds(ids));
    }


    E modelToEntity(V model);

    V entityToModel(E entity);

}
